from optimum.onnxruntime  import ORTModelForSpeechSeq2Seq
import torch
from transformers import AutoModelForSpeechSeq2Seq, AutoProcessor, pipeline


device = "cuda:0" if torch.cuda.is_available() else "cpu"
torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32

model_id = 'openai/whisper-large-v3-turbo'

import os
import shutil

#  如果存在 openai文件夹
if os.path.exists("openai"):
    print('openai folder exists, 正在删除中')
    shutil.rmtree("openai")

print('Loading model...')
model = ORTModelForSpeechSeq2Seq.from_pretrained(model_id,
                                                 export=True,
                                                 cache_dir='./data-cache')

model.save_pretrained(model_id)
processor = AutoProcessor.from_pretrained(model_id,)

print('Loading dataset...')
pipe = pipeline(
    'automatic-speech-recognition',
    model=model,
    tokenizer=processor.tokenizer,
    feature_extractor=processor.feature_extractor,
    torch_dtype=torch_dtype,
    device=device,
)

def get_audio_data(audio_path: str) -> str:
    print('Getting audio data...'+audio_path)
    result =pipe(audio_path, generate_kwargs={'language': 'chinese'})
    print('Audio data got.')
    print(result)
    return result['text']


if __name__ == '__main__':
    print(get_audio_data('/Users/luckincoffee/Desktop/project/python/aigc-python/english.mp3'))
